import requests
import os
def crawl_pxu_website():
    # 目标URL
    url = "http://www.pxc.jx.cn/"

    # 设置请求头，进行UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        # 发送HTTP请求
        response = requests.get(url, headers=headers)
        response.encoding = 'utf-8'  # 设置正确的编码

        # 检查请求是否成功
        if response.status_code == 200:
            # 创建保存目录（如果不存在）
            save_dir = "D:\\"
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)

            # 保存文件路径
            save_path = os.path.join(save_dir, "萍乡学院.html")

            # 以二进制写入模式保存文件，确保编码正确
            with open(save_path, 'wb') as f:
                f.write(response.content)

            print(f"页面已成功保存到: {save_path}")
        else:
            print(f"请求失败，状态码: {response.status_code}")

    except requests.exceptions.RequestException as e:
        print(f"请求过程中发生错误: {e}")
    except IOError as e:
        print(f"文件保存过程中发生错误: {e}")


if __name__ == "__main__":
    crawl_pxu_website()